using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ImdbRipperBrowser
{
  public class CapacityList : IList<string>
  {
    public CapacityList(int capacity, string separator)
      : base()
    {
      this.separator = separator;
      this.capacity = capacity;
    }

    private string separator;

    private bool toStringInvalidated = true;
    private string toStringValue = string.Empty;

    public override string ToString()
    {
      if (toStringInvalidated)
      {
        string aux = string.Empty;

        foreach (var item in internalList)
        {
          var itemSplitted = item.Split(new string[1] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
          aux += itemSplitted[0] + separator;
        }

        if (!string.IsNullOrEmpty(aux))
          aux = aux.Substring(0, aux.Length - separator.Length);

        toStringValue = aux;
        toStringInvalidated = false;
      }

      return toStringValue;
    }

    private int capacity;

    private IList<string> internalList = new List<string>();

    public int IndexOf(string item)
    {
      return internalList.IndexOf(item);
    }

    public void Insert(int index, string item)
    {
      throw new NotImplementedException();
    }

    public void RemoveAt(int index)
    {
      throw new NotImplementedException();
    }

    public string this[int index]
    {
      get
      {
        return internalList[index];
      }
      set
      {
        toStringInvalidated = true;
        internalList[index] = value;
      }
    }

    public void Add(string item)
    {
      if (internalList.Count < capacity)
      {
        toStringInvalidated = true;
        internalList.Add(item);
      }
    }

    public void Clear()
    {
      toStringInvalidated = true;
      internalList.Clear();
    }

    public bool Contains(string item)
    {
      return internalList.Contains(item);
    }

    public void CopyTo(string[] array, int arrayIndex)
    {
      throw new NotImplementedException();
    }

    public int Count
    {
      get
      {
        return internalList.Count;
      }
    }

    public bool IsReadOnly
    {
      get { throw new NotImplementedException(); }
    }

    public bool Remove(string item)
    {
      toStringInvalidated = true;
      return internalList.Remove(item);
    }

    public IEnumerator<string> GetEnumerator()
    {
      return internalList.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
      return internalList.GetEnumerator();
    }
  }
}
